package com.hckjsec.law.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.law.domain.WebLawPropaganda;
import com.hckjsec.law.service.IWebLawPropagandaService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公法小程序法制宣传Controller
 * 
 * @author cyt
 * @date 2021-11-11
 */
@RestController
@RequestMapping("/system/lawpropaganda")
public class WebLawPropagandaController extends BaseController
{
    @Autowired
    private IWebLawPropagandaService webLawPropagandaService;

    /**
     * 查询公法小程序法制宣传列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebLawPropaganda webLawPropaganda)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webLawPropaganda.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawPropaganda> list = webLawPropagandaService.selectWebLawPropagandaList(webLawPropaganda);
        return getDataTable(list);
    }

    /**
     * 导出公法小程序法制宣传列表
     */
    @Log(title = "公法小程序法制宣传", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebLawPropaganda webLawPropaganda)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webLawPropaganda.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawPropaganda> list = webLawPropagandaService.selectWebLawPropagandaList(webLawPropaganda);
        ExcelUtil<WebLawPropaganda> util = new ExcelUtil<WebLawPropaganda>(WebLawPropaganda.class);
        return util.exportExcel(list, "propaganda");
    }

    /**
     * 获取公法小程序法制宣传详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webLawPropagandaService.selectWebLawPropagandaById(id));
    }

    /**
     * 新增公法小程序法制宣传
     */
    @Log(title = "公法小程序法制宣传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebLawPropaganda webLawPropaganda)
    {
        if (!"".equals(StringUtils.isHcNull(webLawPropaganda.getContent(),""))){//解密
            webLawPropaganda.setContent(Base64Converter.decode(webLawPropaganda.getContent()));
        }else{
            webLawPropaganda.setContent(webLawPropaganda.getContent());
        }
        webLawPropaganda.setOperatortime(DateUtils.getNowDate());
        webLawPropaganda.setDeptId(SecurityUtils.getDeptId());
        webLawPropaganda.setOperator(SecurityUtils.getUserId());
        return toAjax(webLawPropagandaService.insertWebLawPropaganda(webLawPropaganda));
    }

    /**
     * 修改公法小程序法制宣传
     */
    @Log(title = "公法小程序法制宣传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebLawPropaganda webLawPropaganda)
    {
        if (!"".equals(StringUtils.isHcNull(webLawPropaganda.getContent(),""))){//解密
            webLawPropaganda.setContent(Base64Converter.decode(webLawPropaganda.getContent()));
        }else{
            webLawPropaganda.setContent(webLawPropaganda.getContent());
        }
        return toAjax(webLawPropagandaService.updateWebLawPropaganda(webLawPropaganda));
    }

    /**
     * 删除公法小程序法制宣传
     */
    @Log(title = "公法小程序法制宣传", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webLawPropagandaService.deleteWebLawPropagandaByIds(ids));
    }
}
